// pages/myPublish/myPublish.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    varlist:["在卖","已被下单","完成交易","主动下架"],
    pageIndex:[1,1,1,1],
    current:0,
    datalist:[],
    // 是否加载完毕
    isAddOver:false,
  },
//  --商品状态--
handleState(evt){
  const myid=evt.target.dataset.myid;
  if(myid===3){
    this.handleAjax(-1,1)
  }
  else{
    this.handleAjax(myid,1)
  }
  this.setData({
    current:myid
  })
},
// 触底刷新
handleToLower(e){
  //console.log('触底刷新')
  if(this.data.isAddOver){
    return;
  }
  const that=this;
  const token = wx.getStorageSync('cookie');
  let state = e.currentTarget.dataset.state;
  let page = ++this.data.pageIndex[state];
  console.log('触底',e.currentTarget.dataset.state,page);
  let datalist = this.data.datalist;
  let mystate=state
  if(state===3){
    mystate=-1
  }
  //请求数据
  wx.request({
    url:'http://gzhu.a.vaiwan.com/getMyGoodsList?state='+mystate+'&pageNum='+page+'&access_token='+token,
    method:"GET",
    deta:{
      
    },
    header: {
      'Content-Type':'application/x-www-form-urlencoded'
    },
    success:(res)=>{
      console.log('触底刷新',res);
      if(res.data.code===200){
        let data = [...datalist,...res.data.data];
        console.log(data);
        that.setData({
          datalist:data
        })
      }else{
        this.setData({
          isAddOver:true
        })
      }
    },
    fail:()=>{
      console.log("请求我的发布页数据失败")
    }
  })
}, 

//---数据请求(state)----
handleAjax(state=0,page=1){
  const that=this;
  const token = wx.getStorageSync('cookie');
  wx.showLoading({
    title: '数据加载中',
    mask: false,
  });
  wx.request({
    url:'http://gzhu.a.vaiwan.com/getMyGoodsList?state='+state+'&pageNum='+page+'&access_token='+token,
    method:"GET",
    deta:{
      
    },
    header: {
      'Content-Type':'application/x-www-form-urlencoded'
    },
    success:(res)=>{
      console.log(res);
      wx.hideLoading();
      that.setData({
        datalist:res.data.data,
        isAddOver:false
      })
    },
    fail:()=>{
      console.log("请求我的发布页数据失败")
    }
  })
},
/**
 * 生命周期函数--监听页面加载
 */
onLoad: function (options) {
  this.handleAjax()
  //this.data.current,1
},

// ----主动下架-----
ifCancelSell(e){
  //确认删除的弹窗
  const goodsid=e.currentTarget.dataset.goodsid;
  const that = this;
  wx.showModal({
    title: '提示',
    content: '确定下架该商品？',
    confirmColor: '#3cba92',
    success (res) {
      if (res.confirm) {
        that.cancelSell(goodsid);
      } else if (res.cancel) {
        
      }
    }
  });
},
cancelSell(e){
  const token = wx.getStorageSync('cookie');
  let {list} = this.data;
  const that = this;
  console.log('主动下架',e);
  wx.request({
    url: `http://gzhu.a.vaiwan.com/updateGoodsState?access_token=${token}`,///${e}
    data: {
        "goodsId":e,
        "state":-1
    },
    method: 'POST',
    success: (result) => {
      console.log('下架成功',result);
      if(result.statusCode!==200){
        console.log('下架失败');
        return;
      }
      // 刷新列表里相应的数据
      this.handleAjax(0)
    },
  });
},
//---编辑在卖商品信息----
handleEdit(e){
  const app = getApp();
  wx.showModal({
    title: '提示',
    content: '编辑商品信息需重新选择本地图片上传，是否继续？',
    confirmText: '继续编辑',
    confirmColor: '#3CC51F',
    success (res) {
      if (res.confirm) {
        //给全局变量赋值 
        app.globalData.edit_goodsid = e.currentTarget.dataset.goodsid;
        // 执行跳转
        wx.reLaunch({
          url:`/pages/publish/publish?key=123`,
        })
      }else if (res.cancel) {
        app.globalData.edit_goodsid = -1
      }
    }
  });
},
// 跳转到发布页,处理没有在卖商品时的跳转
navToPublish(){
  wx.switchTab({
    url: `/pages/publish/publish`,
  });
},

//商品列表可跳转到详情页
navToGoodsDetail(e){
  const good_id = e.currentTarget.dataset.goodsid;
  wx.navigateTo({
    url: `/pages/goods_detail/goods_detail?goodsId=${good_id}`,
  });   
}
})